package cn.tedu.v6;

import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;
/**
 * 客户端：登录与消息广播不冲突 √ 退出
 */
public class Client {
    private static  final String HOST="127.0.0.1";
    private static  final Integer PORT=4545;
    private Socket socket;
    private String ip;
    private PrintWriter pw;
    private BufferedReader br;
    public Client(){
        try {
            socket=new Socket(HOST,PORT);
            ip=socket.getInetAddress().getHostAddress();
            br=new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF8"));
            pw=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),"UTF8")),true);
        }catch (SocketException e){
            System.out.println();
        } catch(UnknownHostException e){
            System.out.println("IP地址有问题 请询问服务器端");
        } catch (IOException e) {
            System.out.println("客户端主动退出：断开连接");
        }
    }
    public void start(){
        //与服务器端交互---参考Server
        System.out.println("欢迎使用宇航员牌聊天室");
        Scanner input =new Scanner(System.in);
        try {
            //1.读入服务器端："请输入用户名："
            String severMessage=br.readLine();
            //1.1显示“请输入用户名：”
            System.out.println(severMessage);
            //1.2写出用户名
            String username=input.nextLine();//客户端输入用户名
            //1.3用户输入的用户名--写出给服务器端
            pw.println(username);
            //2.1读入服务器端发给
            severMessage=br.readLine();
            System.out.println(severMessage);
            //2.2 用户输入--密码--写出给服务器端
            String password=input.nextLine();
            pw.println(password);


            //3.登录之后【登陆成功、登陆失败】--才可以聊天
            severMessage=br.readLine();
            System.out.println(severMessage);
            //字符串方法：startsWith() 返回值 boolean 前缀名称一致true
            if (severMessage.startsWith("登录成功")){
                chat(input);
            }
            if (severMessage.equalsIgnoreCase("exit")){
                System.exit(0);
            }
        }catch (IOException e){
            System.out.println("客户端断开连接");
        }
        finally {if (br!=null)
            try {
                br.close();
            } catch (IOException e) {
                System.out.println("服务器通讯失败");
            }
            if (pw!=null)
                pw.close();
            if (socket!=null&&!socket.isClosed()){
                System.out.println("客户端关闭成功");
                try {
                    socket.close();
                } catch (IOException e) {
                    System.out.println("客户端关闭失败");
                }
            }
        }

    }
    //封装一个针对聊天功能：读写 chat()
    public void chat(Scanner scanner) {
        new Thread(() -> {
            try {
                String message;
                while ((message = br.readLine()) != null) {
                    System.out.println("客户端说：" + message);
                    System.out.println("客户端请输入：");
                }
            } catch (UnsupportedEncodingException e) {
                System.out.println();
            } catch (SocketException e) {
                System.out.println();
            } catch (IOException e) {
                System.out.println("服务器端发来的消息断开了");
            }
        }).start();
        System.out.println("客户端请输入：");
        Scanner input = new Scanner(System.in);   //测试----控制台输入文本
        while (true) {//死循环---一直与服务器说话
            String say = input.nextLine();
            pw.println("客户端说：" + say);  //写出   一行行文字
            System.out.println("客户端请输入：");
            if (say.equalsIgnoreCase("exit")){
                break;
            }
        }
    }
    public static void main(String[] args)  {
        new Client().start();
    }
}